package com.hxy.apartment.service.impl;

import com.hxy.apartment.dao.*;
import com.hxy.apartment.entities.*;
import com.hxy.apartment.enums.SignStatusEnum;
import com.hxy.apartment.service.FaceService;
import com.hxy.apartment.utils.Util;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FaceServiceImpl implements FaceService {

    @Resource
    private FaceDao faceDao;

    @Resource
    private ResidentDao residentDao;

    @Resource
    private SignDao signDao;

    @Resource
    private ResidentSignDao residentSignDao;

    @Resource
    private UserDao userDao;


    @Override
    public Result login(User user) {
        List<User> userList = userDao.getUserByUsernameAndPassword(user);
        if (userList.size() > 0) {
            return Result.success("success", userList.get(0));
        }
        return Result.error("error");
    }

    @Override
    public int register(String image, Resident resident) {
        JSONObject res = faceDao.register(image, resident);
        String faceToken;
        try {
            faceToken = (String) ((JSONObject) res.get("result")).get("face_token");
        } catch (Exception e) {
            return 0;
        }
        resident.setFaceToken(faceToken);
        return residentDao.save(resident);
    }

    @Override
    public Result check(String image) {
        JSONObject res = faceDao.check(image);
        System.out.println(res);
        String userId;
        try {
            userId = (String) ((JSONObject) ((JSONArray) ((JSONObject) res.get("result"))
                    .get("user_list")).get(0)).get("user_id");
        } catch (Exception e) {
            return Result.error("人脸信息未录入");
        }
        List<Resident> residentList = residentDao.getResidentById(userId);
        if (residentList.size() > 0) {
            Resident resident = residentList.get(0);
            Sign sign0 = new Sign(resident.getId(), Util.getDate());
            List<Sign> signList = signDao.findSignByRidAndTime(sign0);
            if (signList.size() > 0) {
                return Result.error(resident.getName() + "同学已签到，请勿重复签到");
            }
            Sign sign = new Sign(resident.getId(), Util.getDate(), SignStatusEnum.SUCCESS.getStatus(), Util.getTime());
            int save = signDao.save(sign);
            if (save != 0) {
                return Result.success(resident.getName() + " 签到成功", sign);
            }
        }
        return Result.error("签到失败，如若多次失败请联系管理员");
    }

    @Override
    public Result vacation(Sign sign) {
        sign.setStatus(SignStatusEnum.VACATION.getStatus());
        sign.setSignTime(Util.getTime());
        List<Sign> signList = signDao.findSignByRidAndTime(sign);
        if (signList.size() > 0) {
            return Result.error(sign.getTime() + "当日的数据已存在，请勿重复添加");
        }
        int save = signDao.save(sign);
        if (save != 0) {
            return Result.success(sign.getRid() + "同学请假成功,请假时间为" + sign.getTime());
        }
        return Result.error("error");
    }

    @Override
    public Result get(String time) {
        List<ResidentSign> signList = residentSignDao.findByTime(time, SignStatusEnum.SUCCESS.getStatus().toString());
        List<ResidentSign> noSignList = residentSignDao.findByTime(time, SignStatusEnum.ERROR.getStatus().toString());
        List<ResidentSign> vacationList = residentSignDao.findByTime(time, SignStatusEnum.VACATION.getStatus().toString());
        GetSignResult getSignResult = new GetSignResult(signList.size(), noSignList.size(), vacationList.size(), signList, noSignList, vacationList);
        return Result.success("SUCCESS", getSignResult);
    }

    @Override
    public int cancelCheck(Integer id) {
        return signDao.deleteSignById(id);

    }
}
